//
//  QCMainViewController.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/1/16.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class QCMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //实例化自定义tabBar 
        let tabBar = QCTabBar()
        
        //实例化闭包
        tabBar.closure = {
            
            let composeView: QCComposeView = QCComposeView()
            
            composeView.show(target: self)
            
        }
        //通过KVC 给只读属性赋值
        
        setValue(tabBar, forKey: "tabBar")
        
        addChildViewController(QCHomeViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(QCMessageViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(QCDiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(QCProfileViewController(), imageName: "tabbar_profile", title: "我")
    }
}
extension QCMainViewController {
    
    func addChildViewController(_ childController: UIViewController , imageName: String , title: String) {
        //设置title
        childController.title = title
        //设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: KCOlOR ], for: UIControlState.selected)
        //设置图片 及 渲染模式
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //添加子控制器
        addChildViewController(QCNavViewController(rootViewController: childController))
    }
}
